package cn.iocoder.yudao.module.enterprise.controller.admin.emissionsource;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.carbon.api.gas.GasApi;
import cn.iocoder.yudao.module.carbon.api.gas.dto.GasRespDTO;
import cn.iocoder.yudao.module.carbon.api.gwp.GwpApi;
import cn.iocoder.yudao.module.carbon.api.gwp.dto.GwpListReqDTO;
import cn.iocoder.yudao.module.carbon.api.gwp.dto.GwpRespDTO;
import cn.iocoder.yudao.module.enterprise.controller.admin.emissionsource.vo.*;
import cn.iocoder.yudao.module.enterprise.convert.emissionsource.EmissionSourceConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.emissionsource.EmissionSourceDO;
import cn.iocoder.yudao.module.enterprise.service.emissionsource.EmissionSourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 排放源")
@RestController
@RequestMapping("/enterprise/emission-source")
@Validated
public class EmissionSourceController {

    @Resource
    private EmissionSourceService emissionSourceService;
    @Resource
    private GasApi gasApi;
    @Resource
    private GwpApi gwpApi;

    @PostMapping("/create")
    @Operation(summary = "创建排放源")
    @PreAuthorize("@ss.hasPermission('enterprise:emission-source:create')")
    public CommonResult<Long> createEmissionSource(@Valid @RequestBody EmissionSourceCreateReqVO createReqVO) {
        return success(emissionSourceService.createEmissionSource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新排放源")
    @PreAuthorize("@ss.hasPermission('enterprise:emission-source:update')")
    public CommonResult<Boolean> updateEmissionSource(@Valid @RequestBody EmissionSourceUpdateReqVO updateReqVO) {
        emissionSourceService.updateEmissionSource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排放源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('enterprise:emission-source:delete')")
    public CommonResult<Boolean> deleteEmissionSource(@RequestParam("id") Long id) {
        emissionSourceService.deleteEmissionSource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得排放源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('enterprise:emission-source:query')")
    public CommonResult<EmissionSourceRespVO> getEmissionSource(@RequestParam("id") Long id) {
        return success(emissionSourceService.getEmissionSource(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得排放源分页")
    @PreAuthorize("@ss.hasPermission('enterprise:emission-source:query')")
    public CommonResult<PageResult<EmissionSourceSimpleRespVO>> getEmissionSourcePage(@Valid EmissionSourcePageReqVO pageVO) {
        PageResult<EmissionSourceDO> pageResult = emissionSourceService.getEmissionSourcePage(pageVO);
        return success(EmissionSourceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出排放源 Excel")
    @PreAuthorize("@ss.hasPermission('enterprise:emission-source:export')")
    @OperateLog(type = EXPORT)
    public void exportEmissionSourceExcel(@Valid EmissionSourceExportReqVO exportReqVO,
                                          HttpServletResponse response) throws IOException {
        List<EmissionSourceDO> list = emissionSourceService.getEmissionSourceList(exportReqVO);
        // 导出 Excel
        List<EmissionSourceExcelVO> datas = EmissionSourceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "排放源.xls", "数据", EmissionSourceExcelVO.class, datas);
    }

    @GetMapping("/list-gas")
    @Operation(summary = "获得排放源的温室气体列表")
    @Parameter(name = "gwpId", description = "gwpId", required = true, example = "6")
    @PreAuthorize("@ss.hasPermission('enterprise:emission-source:query')")
    public CommonResult<List<EmissionGaseRespVO>> getEmissionGasList(@RequestParam("gwpId") Integer gwpId) {
        List<GasRespDTO> gasList = gasApi.getGasList().getCheckedData();
        List<GwpRespDTO> gwpList = gwpApi.getGwpList(new GwpListReqDTO().setReportVersion(gwpId)).getCheckedData();
        List<EmissionGaseRespVO> respList = new ArrayList<>();
        // 拼接gas信息
        for (GasRespDTO gas : gasList) {
            EmissionGaseRespVO respVO = new EmissionGaseRespVO();
            respVO.setCategory(gas.getCategory())
                    .setName(gas.getName())
                    .setGaseId(gas.getId());
            // 拼接gwp信息
            for (GwpRespDTO gwp : gwpList) {
                if (gwp.getGaseId().equals(gas.getId())) {
                    respVO.setGwp(gwp.getGwp());
                }
            }
            respList.add(respVO);
        }
        return success(respList);
    }

}
